<?php
namespace Home\Controller;
use Common\Controller\FirstendController;

class BrandController extends FirstendController 
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('BrandItems');
        $this->_brandmod = D('Brand');
        $this->_cate_mod = D('BrandCate');
        $this->_tagmod = D('Tags');
        $this->cats_list = $this->_cate_mod->get_cats();
        $this->assign('nav_curr', 'brand');
        $this->assign('acats', $this->cats_list);
        $this->brand_per = 12;
    }
    public function index()
    {
        $zym_44 = I('p', 1, 'intval');
        $zym_46 = 'id desc';
        $zym_47['pass'] = '1';
        $zym_49 = 10;
        $zym_48['p'] = $zym_44;
        $zym_43 = $zym_49 * ($zym_44 - 1);
        $zym_42 = $zym_47;
        $zym_42['order'] = $zym_46;
        $zym_42['p'] = $zym_44;
        $cid=I('cid',0,'intval');
        if($cid!=0)
            $zym_47['cate_id']=$cid;
        $zym_37 = $this->_brandmod->where($zym_47)->order($zym_46)->limit($zym_43 . ',' . $zym_49)->select();
        $zym_36 = array();
        foreach ($zym_37 as $zym_35 => $zym_38) {
            $zym_36['item_list'][$zym_35] = $zym_38;
            $zym_39 = $zym_38['activityId'];
            $zym_41['activityId'] = $zym_39;
            $zym_40 = $zym_38['id'];
            $zym_36['item_list'][$zym_35]['counts'] = D('brand_items')->where($zym_41)->count();
            $zym_51 = $zym_36['item_list'][$zym_35]['counts'];
        }
        $this->assign('items_list', $zym_36['item_list']);
        $this->assign('index_info', $zym_48);
        $zym_66 = $this->_brandmod->where($zym_47)->count();
        $zym_61 = $this->_pager($zym_66, $zym_49);
        $this->assign('page', $zym_61->jshow());
        $this->assign('total_item', $zym_66);
        $this->assign('pager', 'index');
        $this->assign('nav_curr', 'brand');
        $this->assign('cid',$cid);
        $this->_config_seo(C('itk_seo_config.brand_index'));
        $this->display('index');
    }
    public function cate()
    {
        $zym_60 = I('cid', '', 'intval');
        $zym_62 = $this->_cate_mod->where(array('id' => $zym_60))->find();
        !$zym_62 && $this->_404();
        $zym_44 = I('p', 1, 'intval');
        $zym_46 = 'id desc';
        $zym_47['pass'] = '1';
        $zym_47['cate_id'] = $zym_60;
        $zym_49 = 20;
        $zym_48['p'] = $zym_44;
        $zym_43 = $zym_49 * ($zym_44 - 1);
        $zym_42 = $zym_47;
        $zym_42['order'] = $zym_46;
        $zym_42['p'] = $zym_44;
        $zym_37 = $this->_brandmod->where($zym_47)->order($zym_46)->limit($zym_43 . ',' . $zym_49)->select();
        $zym_36 = array();
        foreach ($zym_37 as $zym_35 => $zym_38) {
            $zym_36['item_list'][$zym_35] = $zym_38;
            $zym_63 = $zym_38['sellerId'];
            $zym_41['sellerId'] = $zym_63;
            $zym_40 = $zym_38['id'];
            $zym_36['item_list'][$zym_35]['counts'] = $this->_mod->where($zym_41)->count();
            $zym_51 = $zym_36['item_list'][$zym_35]['counts'];
        }
        $this->assign('items_list', $zym_36['item_list']);
        $this->assign('index_info', $zym_48);
        $zym_66 = $this->_brandmod->where($zym_47)->count();
        $zym_61 = $this->_pager($zym_66, $zym_49);
        $this->assign('page', $zym_61->jshow());
        $this->assign('total_item', $zym_66);
        $this->assign('cid', $zym_60);
        $this->assign('pager', 'index');
        $this->_config_seo(C('itk_seo_config.BrandCate'), array('cate_name' => $zym_62['name'], 'seo_title' => $zym_62['seo_title'], 'seo_keywords' => $zym_62['seo_keys'], 'seo_description' => $zym_62['seo_desc']));
        $this->display('cate');
    }
    public function view()
    {
        $zym_65 = I('id', '', 'intval');
        $zym_64 = $this->_brandmod->where(array('id' => $zym_65))->find();
        $zym_59 = $zym_64['activityId'];
        $zym_60 = $zym_64['cate_id'];
        $zym_46 = 'ordid asc ';
        $this->assign('cid', $zym_60);
        $zym_41['activityId'] = $zym_59;
        $zym_41['pass'] = '1';
        $zym_49 = C('itk_index_page_size');
        $zym_44 = I('p', 1, 'intval');
        $zym_48['p'] = $zym_44;
        $zym_43 = $zym_49 * ($zym_44 - 1);
        $zym_37 = $this->_mod->where($zym_41)->order($zym_46)->limit($zym_43 . ',' . $zym_49)->select();
        $zym_36 = array();
        foreach ($zym_37 as $zym_35 => $zym_38) {
            $zym_36['item_list'][$zym_35] = $zym_38;
            $zym_36['item_list'][$zym_35]['cate_name'] = $zym_58['p'][$zym_38['cate_id']]['name'];
        }
        $this->assign('items_list', $zym_36['item_list']);
        $this->assign('index_info', $zym_48);
        $zym_66 = $this->_mod->where($zym_41)->count();
        $zym_61 = $this->_pager($zym_66, $zym_49);
        $this->assign('page', $zym_61->jshow());
        $this->assign('total_item', $zym_66);
        $this->assign('brand', $zym_64);
        $this->_config_seo(C('itk_seo_config.brand_view'), array('title' => $zym_64['title'], 'desc' => $zym_64['desc'], 'seo_title' => $zym_64['title'] . ',' . $zym_64['title'] . '打折活动,' . $zym_64['title'] . '正品保障 - ' . C('itk_site_name'), 'seo_keywords' => $zym_64['title'], '品牌团' . '品牌折扣', 'seo_description' => $zym_64['desc']));
        $this->sellike($zym_64['cate_id']);
        $this->display('view');
    }
    public function item()
    {
        $zym_65 = I('id', '', 'intval');
        $zym_53 = $this->_mod->where(array('id' => $zym_65))->find();
        !$zym_53 && $this->_404();
        $zym_39 = $zym_53['activityId'];
        $zym_64 = $this->_brandmod->where(array('activityId' => $zym_39))->find();
        $zym_52 = $zym_53['num_iid'];
        $zym_54 = D('ItemsLike')->field('id,uid')->limit(0, 6)->where(array('num_iid' => $zym_52))->order('id desc')->select();
        $zym_55 = D('ItemsLike')->where(array('num_iid' => $zym_52))->count();
        $zym_57 = array();
        if ($zym_54) {
            foreach ($zym_54 as $zym_56) {
                $zym_57[] = $zym_56['uid'];
            }
        }
        $zym_34['id'] = array('in', $zym_57);

        if(count($zym_57) != 0)
        {
        	$zym_33 = D('User')->field('id,username,email,qq,last_time')->where($zym_34)->select();
        	foreach ($zym_33 as $zym_35 => $zym_11) {
        		$zym_10[$zym_35] = $zym_11;
        	}     	
        }

        $zym_12 = F('cate_list');
        $zym_60 = $zym_53['cate_id'];
        $zym_13 = $this->_cate_mod->where(array('id' => $zym_60))->find();
        $zym_15 = $zym_12[$zym_60]['pid'];
        if (C('itk_site_cache')) {
            $zym_14 = 'orlike_' . $zym_60;
            if (false === ($zym_9 = S($zym_14))) {
                if ($zym_60) {
                    $zym_9 = $this->_mod->where(array('cate_id' => $zym_60, 'isshow' => '1', 'pass' => '1'))->limit('0,12')->order('id desc')->select();
                } else {
                    $zym_9 = $this->_mod->where(array('isshow' => '1', 'pass' => '1'))->limit('0,12')->order('id desc')->select();
                }
                S($zym_14, $zym_9);
            }
        } else {
            if ($zym_60) {
                $zym_9 = $this->_mod->where(array('cate_id' => $zym_60, 'isshow' => '1', 'pass' => '1'))->limit('0,12')->order('id desc')->select();
            } else {
                $zym_9 = $this->_mod->where(array('isshow' => '1', 'pass' => '1'))->limit('0,12')->order('id desc')->select();
            }
        }
        $zym_36 = array();
        foreach ($zym_9 as $zym_35 => $zym_38) {
            $zym_36[$zym_35] = $zym_38;
        }
        $this->assign('items_list', $zym_36);
        $this->assign('cate_list', $zym_58);
        $zym_8 = D('Items')->get_tags_by_title($zym_53['title']);
        $zym_3 = implode(',', $zym_8);
        
        import("ITK.ORG.Pinyin");
        import("ITK.Itklib.itaoke_https");
        $zym_2 = new \Pinyin();
        $zym_1 = $zym_2->getAllPY($zym_3);
        $zym_1 = explode(',', $zym_1);
        $zym_4 = explode(',', $zym_3);
        $zym_5 = count($zym_4);
        for ($zym_7 = 0; $zym_7 < $zym_5; $zym_7++) {
            $zym_36['name'] = $zym_1[$zym_7];
            $zym_36['name'] = str_replace(' ', '', $zym_36['name']);
            $zym_36['name'] = $zym_4[$zym_7];
            if (!$this->_tagmod->where(array('name' => $zym_36['name']))->count()) {
                $this->_tagmod->add($zym_36);
            }
        }
        $this->_config_seo(C('itk_seo_config.brand_item'), array('title' => $zym_53['title'], 'brand_title' => $zym_64['title'], 'intro' => $zym_53['intro'], 'price' => $zym_53['price'], 'coupon_price' => $zym_53['coupon_price'], 'coupon_rate' => $zym_53['coupon_rate'], 'tags' => $zym_53['onetag'] . ',' . $zym_53['twotag'] . ',' . $zym_53['threetag'], 'volume' => $zym_53['volume'], 'cu' => $zym_53['cu'], 'seo_title' => $zym_53['seo_title'], 'seo_keywords' => $zym_53['seo_keys'], 'seo_description' => $zym_53['seo_desc']));
        if (!$zym_53['desc']) {
            $zym_6 = 'http://hws.m.taobao.com/cache/mtop.wdetail.getItemDescx/4.1/?data=%7B%22item_num_id%22%3A%22' . $zym_53['num_iid'] . '%22%7D';
            $zym_16 = new \itaoke_https();
            $zym_16->fetch($zym_6);
            $zym_17 = $zym_16->results;
            if (!$zym_17) {
                $zym_17 = file_get_contents($zym_6);
            }
            $zym_28 = json_decode($zym_17, true);
            $zym_27 = $zym_28['data']['images'];
            $zym_29 = '<img class="lazy" src=' . $zym_27[0] . '>';
            $zym_53['desc'] = implode('', $zym_27);
            foreach ($zym_27 as $zym_30) {
                if (strpos($zym_53['desc'], $zym_30)) {
                    $zym_32 = '<img class="lazy" src=' . $zym_30 . '>';
                    $zym_53['desc'] = str_replace($zym_30, $zym_32, $zym_53['desc']);
                    $zym_53['desc'] = str_replace($zym_27[0], '', $zym_53['desc']);
                }
            }
            $zym_53['desc'] = $zym_29 . '' . $zym_53['desc'];
            $zym_31['desc'] = $zym_53['desc'];
            $this->_mod->where(array('id' => $zym_65))->save($zym_31);
        }
        $zym_26 = 'http://hws.m.taobao.com/cache/wdetail/5.0/?id=' . $zym_53['num_iid'];
        $zym_25 = curl_init();
        curl_setopt($zym_25, CURLOPT_URL, $zym_26);
        curl_setopt($zym_25, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($zym_25, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($zym_25, CURLOPT_MAXREDIRS, 2);
        $zym_20 = curl_exec($zym_25);
        curl_close($zym_25);
        if (!$zym_20) {
            $zym_20 = file_get_contents($zym_26);
        }
        $zym_19 = json_decode($zym_20, true);
        $zym_18 = array();
        $zym_21 = json_decode($zym_19['data']['apiStack'][0]['value'], true);
        $zym_18['title'] = $zym_19['data']['itemInfoModel']['title'];
        $zym_18['volume'] = $zym_21['data']['itemInfoModel']['totalSoldQuantity'];
        $zym_18['pai'] = $zym_21['data']['itemInfoModel']['priceUnits'][0]['name'];
        $zym_18['pai'] = mb_substr($zym_18['pai'], 0, 2, 'utf-8');
        $zym_18['coupon_price'] = $zym_21['data']['itemInfoModel']['priceUnits'][0]['price'];
        if (substr_count($zym_18['coupon_price'], '-')) {
            $zym_22 = explode('-', $zym_18['coupon_price']);
            $zym_18['coupon_price'] = min($zym_22[0], $zym_22[1]);
        }
        $zym_18['price'] = $zym_21['data']['itemInfoModel']['priceUnits'][1]['price'];
        if (substr_count($zym_18['price'], '-')) {
            $zym_21 = explode('-', $zym_18['price']);
            $zym_18['price'] = min($zym_21[0], $zym_21[1]);
        }
        $zym_18['pic_url'] = $zym_19['data']['itemInfoModel']['picsPath'][0];
        $zym_18['pic_url'] = str_replace('_320x320.jpg', '', $zym_18['pic_url']);
        $zym_18['sellerId'] = $zym_19['data']['seller']['userNumId'];
        $zym_18['nick'] = $zym_19['data']['seller']['nick'];
        $zym_18['feedback'] = $zym_19['data']['rateInfo']['rateDetailList'][0]['feedback'];
        $zym_18['nicker'] = '买家【<span style="color:#FF0000">' . $zym_19['data']['rateInfo']['rateDetailList'][0]['nick'] . '</span>】评价说：';
        $zym_18['headPic'] = $zym_19['data']['rateInfo']['rateDetailList'][0]['headPic'];
        $zym_18['feedback1'] = $zym_19['data']['rateInfo']['rateDetailList'][1]['feedback'];
        $zym_18['nicker1'] = '买家【<span style="color:#0000FF">' . $zym_19['data']['rateInfo']['rateDetailList'][1]['nick'] . '</span>】评价说：';
        $zym_18['headPic1'] = $zym_19['data']['rateInfo']['rateDetailList'][1]['headPic'];
        $zym_18['feedback2'] = $zym_19['data']['rateInfo']['rateDetailList'][2]['feedback'];
        $zym_18['nicker2'] = '买家【<span style="color:#800080">' . $zym_19['data']['rateInfo']['rateDetailList'][2]['nick'] . '</span>】评价说：';
        $zym_18['headPic2'] = $zym_19['data']['rateInfo']['rateDetailList'][2]['headPic'];
        $zym_18['feedback3'] = $zym_19['data']['rateInfo']['rateDetailList'][3]['feedback'];
        $zym_18['nicker3'] = '买家【<span style="color:#008000">' . $zym_19['data']['rateInfo']['rateDetailList'][3]['nick'] . '</span>】评价说：';
        $zym_18['headPic3'] = $zym_19['data']['rateInfo']['rateDetailList'][3]['headPic'];
        $zym_18['feedback4'] = $zym_19['data']['rateInfo']['rateDetailList'][4]['feedback'];
        $zym_18['nicker4'] = '买家【<span style="color:#FF8000">' . $zym_19['data']['rateInfo']['rateDetailList'][4]['nick'] . '</span>】评价说：';
        $zym_18['headPic4'] = $zym_19['data']['rateInfo']['rateDetailList'][4]['headPic'];
        $zym_18['type'] = $zym_19['data']['seller']['type'];
        if ($zym_18['type'] == 'B') {
            $zym_24 = 'http://detail.tmall.com/item.htm?id=' . $zym_53['num_iid'];
            $zym_16 = new \itaoke_https();
            $zym_16->fetch($zym_24);
            $zym_23 = $zym_16->results;
            $zym_23 = iconv('GBK', 'UTF-8//IGNORE', $zym_23);
            $zym_18['ku'] = get_word($zym_23, '<ul id="J_AttrUL">', '<\\/ul>');
        } else {
            $zym_24 = 'http://item.taobao.com/item.htm?id=' . $zym_53['num_iid'];
            $zym_16 = new \itaoke_https();
            $zym_16->fetch($zym_24);
            $zym_23 = $zym_16->results;
            $zym_23 = iconv('GBK', 'UTF-8//IGNORE', $zym_23);
            $zym_18['ku'] = get_word($zym_23, '<ul class="attributes-list">', '<\\/ul>');
        }
        $this->assign('info', $zym_18);
        $this->assign('item', $zym_53);
        $this->assign('brandcate', $zym_13);
        $this->assign('brand', $zym_64);
        $this->assign('tags', $zym_8);
        $this->assign('whos', $zym_10);
        $this->assign('wholike', $zym_55);
        $this->display('item');
    }
    
    
    public function sellike($cid){
        $like['isshow'] = '1';
        $like['cate_id'] = $cid;
        $brands = M('brand')->where($like)->limit('0,' . $this->brand_per)->select();
        $this->assign('brands',$brands);
    }
}